﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using Microsoft.SqlServer.Server;

using System.Collections.Generic;

namespace SqlXslt {
    public class XsltUtilities {
        [Microsoft.SqlServer.Server.SqlProcedure]
        public static void TestXslt(SqlXml xslt) {
            XslCompiledTransform xs = new XslCompiledTransform();
            xs.Load(xslt.CreateReader());
        }

        [Microsoft.SqlServer.Server.SqlFunction]

        public static SqlString XsltTransformer(SqlXml sXml, SqlXml xslt) {
            XmlDocument xd = new XmlDocument();
            XslCompiledTransform xs = new XslCompiledTransform();

            //xd.LoadXml();
            //xd.Load(sXml.CreateReader());
            xs.Load(xslt.CreateReader(), new XsltSettings(true, true), null);
            

            StringBuilder sb = new StringBuilder();
            TextWriter tw = new StringWriter(sb);

            //xs.Transform(xd, null, tw);
            
            xs.Transform(sXml.CreateReader(), null, tw);
            return new SqlString(sb.ToString());
        }
    }
}
